<script lang="ts" setup>
import BaseButton from "@/components/BaseButton/index.vue";
import BaseUpDown from "@/components/BaseUpDown/index.vue";
import SelectThreatInput from "@/components/SelectThreatInput/index.vue";
import useSearch from "@/hooks/useSearch";
import useData from "./hooks/useData";
import { useUserStore } from "@/stores/modules/user";
import DateRange from "@/components/DateRange/index.vue";
import SelectIfaceInput from "@/components/SelectIfaceInput/index.vue";
import { PORT1 } from "@/api/config/servicePort";
import { onUnmounted, ref } from "vue";

const { pageData, roleMap } = useData();
import { getDataByPage, getIfaceLink, getIfaceInfo } from "@/api/modules/system-monitor/host-monitor";
import UpdateForm from "./components/UpdateForm/index.vue";
import UpdateFormPassword from "./components/UpdateFormPassword/index.vue";

const userStore = useUserStore();
const username = computed(() => userStore.userInfo.name);
const { tableData, loadData, formData, reloadData } = useSearch({
  defaultData: {
    ifname: 0
  },
  responseKey: {
    list: "result.data",
    total: "result.total"
  }
});

function loadDataCb() {
  loadData({
    api: getDataByPage,
    sendData: {
      curpage: formData.pageNum - 1,
      perpage: formData.pageSize,
      ifname: formData.ifname,
      mac: formData.mac,
      vlanid: formData.vlanid,
      ip: formData.ip
    }
  });
}

loadDataCb();

function reloadDataCb() {
  reloadData();
  loadData({
    api: getDataByPage,
    sendData: {
      pageNum: formData.pageNum - 1,
      pageSize: formData.pageSize
    }
  });
}

loadDataCb();
const timer = ref();

function startLoad() {
  if (timer.value) {
    clearInterval(timer.value);
    timer.value = null;
  }
  timer.value = setInterval(() => {
    loadDataCb();
  }, 1500);
}

startLoad();
onUnmounted(() => {
  if (timer.value) {
    clearInterval(timer.value);
    timer.value = null;
  }
});
/**删除*/
import useDelete from "@/hooks/useDelete";
import { computed } from "vue";
import BaseCollapse from "@/components/BaseCollapse/index.vue";

function download(url) {
  return new Promise((resolve, reject) => {
    const iframe = document.createElement("iframe");
    iframe.style.display = "none";

    function iframeLoad() {
      console.log("iframe onload");
      const win = iframe.contentWindow;
      const doc = win.document;
      if (win.location.href === url) {
        if (doc.body.childNodes.length > 0) {
          // response is error
        }
        iframe.parentNode.removeChild(iframe);
      }
    }

    if ("onload" in iframe) {
      iframe.onload = iframeLoad;
    } else if (iframe.attachEvent) {
      iframe.attachEvent("onload", iframeLoad);
    } else {
      iframe.onreadystatechange = function onreadystatechange() {
        if (iframe.readyState === "complete") {
          iframeLoad;
        }
      };
    }
    iframe.src = "";
    document.body.appendChild(iframe);

    setTimeout(function loadUrl() {
      iframe.contentWindow.location.href = url;
      resolve();
    }, 150);
  });
}

const lansNode = ref([]);

async function getLinksHas() {
  const res = await getIfaceLink();
  console.log("getLinksHas", res);
  const data = res.find(item => item.linkmode == "串联模式");
  console.log("data===", data);
  const nodeInfo = await getIfaceInfo({
    linkid: data.linkid
  });
  console.log("nodeInfo===", nodeInfo);
  const result = nodeInfo.data.filter(item => item.type == "串联LAN口");
  console.log("result===", result);
  if (result && result.length > 0) {
    lansNode.value = result.map((item, index) => {
      return item.ifname;
    });
  }
}

getLinksHas();
</script>

<template>
  <BaseUpDown>
    <template #up>
      <BaseCollapse title="">
        <el-form label-position="right">
          <div class="h-box">
            <el-form-item label-width="72px" label="MAC">
              <el-input placeholder="请输入" style="width: 140px" v-model="formData.mac"></el-input>
            </el-form-item>
            <el-form-item label-width="72px" label="IP">
              <el-input placeholder="请输入" style="width: 140px" v-model="formData.sip"></el-input>
            </el-form-item>
            <el-form-item label-width="72px" label="网口">
              <SelectIfaceInput requestAll placeholder="所有" width="140px" v-model="formData.ifname" />
            </el-form-item>
            <BaseButton class="mg-l-sm" style="margin-bottom: 18px" @click="loadDataCb()" type="search_icon_text" />
            <BaseButton class="mg-l-sm" style="margin-bottom: 18px" @click="reloadDataCb()" type="reset_icon_text" />
          </div>
        </el-form>
      </BaseCollapse>
    </template>
    <he-table
      @handle-current-change="loadDataCb"
      :total="tableData.total"
      v-model:page-size="formData.pageSize"
      v-model:page-num="formData.pageNum"
      operate-width="180px"
      :operate-list="[]"
      :table-data="tableData.list"
      :column-data="pageData.columnData"
      :show-multiple="false"
      :show-summary="false"
      :show-operate="false"
      :show-multiple-column="false"
      :show-pagination="true"
    >

      <template #8Default="{ data }">
        {{ data.row[7] + "/" + data.row[8] }}
      </template>
    </he-table>
  </BaseUpDown>
</template>
